package cn.itcast.health.mobile.controller;

import cn.itcast.health.commons.entity.Result;
import cn.itcast.health.commons.resources.MessageConstant;
import cn.itcast.health.commons.resources.RedisMessageConstant;
import cn.itcast.health.commons.sms.SMSUtils;
import cn.itcast.health.commons.sms.ValidateCodeUtils;
import cn.itcast.health.commons.utils.LogUtils;
import cn.itcast.health.interfaces.service.UserMobileService;
import cn.itcast.health.mobile.logs.OperationLog;
import cn.itcast.health.pojo.Member;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("userMobile")
public class UserMobileController {

    @Reference
    private UserMobileService userMobileService;

    @Autowired
    private JedisPool jedisPool;

    @PostMapping("verifyLogin.do")
    @OperationLog(moduleName = LogUtils.Module.MOBILE, operationType = LogUtils.Type.AUDIT, operationContent = "移动端根据手机号审核当前用户是否登录")
    public Result verifyLogin(@RequestBody Member member){
        Result result = null;
        try {
            //获取手机号
            String telephone = member.getPhoneNumber();
            Jedis redis = jedisPool.getResource();
            String user = redis.get(telephone + "_" + RedisMessageConstant.SENDTYPE_USER);
            if(user != null){
                result = new Result(true, MessageConstant.GET_USER_SUCCESS,user);
            }else {
                result = new Result(false,MessageConstant.NOT_LOGGED_IN);
            }
        }catch (Exception e){
            e.printStackTrace();
            result = new Result(false,MessageConstant.GET_USER_FAIL);
        }
        return  result;
    }

    @PostMapping("outLogin.do")
    @OperationLog(moduleName = LogUtils.Module.MOBILE, operationType = LogUtils.Type.LOGOUT, operationContent = "移动端根据手机号用户退出删除redis存的用户信息")
    public Result outLogin(@RequestBody Map map){
        //获取手机号
        String telephone = (String) map.get("telephone");
        Jedis redis = jedisPool.getResource();
        redis.del(telephone + "_" + RedisMessageConstant.SENDTYPE_USER);
        return  new Result(true,MessageConstant.OUTLOGGED);
    }


    @PostMapping("login.do")
    @OperationLog(moduleName = LogUtils.Module.MOBILE, operationType = LogUtils.Type.LOGIN, operationContent = "移动端根据手机号及验证码用户登录")
    public Result login(@RequestBody Map map){
        //获取手机号
        String telephone = (String) map.get("telephone");
        String code = (String) map.get("code");
        Jedis redis = jedisPool.getResource();
        String verifyCode = redis.get(telephone + "_" + RedisMessageConstant.SENDTYPE_LOGIN);
        if(verifyCode == null || verifyCode.trim() == ""){
            return  new Result(false,"验证码过期");
        }else if(!code.equals(verifyCode)){
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
        Result result = userMobileService.login(telephone);
        if(result.isFlag()){
            jedisPool.getResource().set(telephone + "_" + RedisMessageConstant.SENDTYPE_USER, (String) result.getData());
        }
        return result;
    }


    @PostMapping("/sendCodeToPhone.do")
    @OperationLog(moduleName = LogUtils.Module.MOBILE, operationType = LogUtils.Type.AUDIT, operationContent = "移动端登录根据手机号审核发送短信验证码")
    public Result sendCodeToPhone(@RequestBody Map map){
        Result result = null;
        try {
            //获取手机号
            String telephone = (String) map.get("telephone");
            //获取验证码
            Integer integer = ValidateCodeUtils.generateValidateCode(6);
            //获取redis
            Jedis resource = jedisPool.getResource();
            resource.setex(telephone+"_"+ RedisMessageConstant.SENDTYPE_LOGIN,90, String.valueOf(integer));
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone, String.valueOf(integer));
            System.out.println("验证码："+String.valueOf(integer));
            result = new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(true, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        return result;
    }

}
